
package com.android.formula.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.formula.R;
import com.android.formula.model.FormulaNote;
import com.android.formula.model.TypeFormula;
import com.android.formula.task.GetNoteFormulaDBTask;
import com.android.formula.task.IDataEventHandler;
import com.android.formula.utils.CommonUtils;
import com.android.formula.utils.Constants;
import com.google.android.gms.ads.AdListener;

public class NoteDetailActivity extends BaseActivity {
    private TextView mDesTv;

    private ImageView mNoteImg;

    private ScrollView mScrollView;

    @Override
    public void initView() {
        setContentView(R.layout.activity_note_detail);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        mDesTv = (TextView) findViewById(R.id.note_detail_des);
        mNoteImg = (ImageView) findViewById(R.id.note_detail_img);
        mScrollView = (ScrollView) findViewById(R.id.note_detail_scrollview);

        setAdListener();
    }

    @Override
    public void loadDataScreen() {
        Bundle bun = getIntent().getExtras();
        TypeFormula params = (TypeFormula) bun.getSerializable(Constants.DATA_KEY);
        this.setTitle(params.getTypeName());

        new GetNoteFormulaDBTask(this, params, new IDataEventHandler<FormulaNote>() {

            @Override
            public void onNotifyNoData() {
            }

            @Override
            public void onNotifyErrorMessage(String title, String message) {
            }

            @Override
            public void onNotifyData(FormulaNote data) {
                if (data != null) {
                    mDesTv.setText(data.getFormulaDes());
                    mNoteImg.setImageBitmap(CommonUtils.decodeBitmap(NoteDetailActivity.this,
                            data.getFormulaImg()));
                }
            }
        }).execute(params);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void setAdListener() {
        final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        mAdListener = new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                super.onAdFailedToLoad(errorCode);
                mAdView.setVisibility(View.GONE);
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mAdView.setVisibility(View.VISIBLE);
                params.addRule(RelativeLayout.ABOVE, R.id.note_detail_layout_ads);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
                mScrollView.setLayoutParams(params);
            }
        };
    }
}
